﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace wpf_print.utils
{
    internal class HttpServe
    {
        public static readonly string BaseUri = "https://xxx.xxx/";
        #region GET请求--异步方法

        /// <summary>
        /// GET请求--异步方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="action">方法</param>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public static async Task<T?> TryGetAsync<T>(string action, Dictionary<string, string> param)
        {
            using HttpClient client = new();
            //基地址/域名
            client.BaseAddress = new Uri(BaseUri);
            //设定传输格式为json
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
            //client.DefaultRequestHeaders.Add("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");

            StringBuilder strUri = new();
            //方法
            strUri.AppendFormat("{0}?", action);
            //参数
            if (param.Count > 0)
            {
                foreach (KeyValuePair<string, string> pair in param)
                {
                    strUri.AppendFormat("{0}={1}&&", pair.Key, pair.Value);
                }
                strUri.Remove(strUri.Length - 2, 2); //去掉'&&'
            }
            else
            {
                strUri.Remove(strUri.Length - 1, 1); //去掉'？'
            }
            HttpResponseMessage response = await client.GetAsync(strUri.ToString());
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsAsync<T>();
            }
            return default;
        }

        #endregion

        #region 无参数GET请求--异步方法

        /// <summary>
        /// 无参数GET请求--异步方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="action"></param>
        /// <returns></returns>
        public static async Task<T?> TryGetAsync<T>(string action)
        {
            using HttpClient client = new();
            //基地址/域名
            client.BaseAddress = new Uri(BaseUri);
            //设定传输格式为json
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.GetAsync(action);
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadAsAsync<T>();
            }
            return default;
        }

        #endregion
        #region POST请求--异步方法

        /// <summary>
        /// POST请求--异步方法
        /// </summary>
        /// <param name="action">方法</param>
        /// <param name="param">参数</param>
        /// <returns></returns>
        public static async Task<T?> TryPostAsync<T>(string action, object param)
        {
            using HttpClient client = new();
            //基地址/域名
            client.BaseAddress = new Uri(BaseUri);
            //设定传输格式为json
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.PostAsync(action, param, new JsonMediaTypeFormatter()).ConfigureAwait(false);
            if (response.IsSuccessStatusCode)
            {

                return await response.Content.ReadAsAsync<T>();
            }
            else
            {
                return default;

            }
        }

        #endregion
    }
}
